package repositories

import (
	"gitee.com/cjf8134/bbs_serve/internal/pkg/elastic"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/models"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/requests"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/constutil"
	"github.com/jinzhu/gorm"
	"go.uber.org/zap"
	"strconv"
)

// ThreadList  帖子列表
func (r *MysqlApiServeRepository) ThreadList(req *requests.ThreadList) (count int64, threadList []*models.Thread, err error) {
	model := r.db.Model(models.Thread{})
	order := "id desc"
	if req.CategoryID != "" {
		model = model.Where("tag = ?", req.CategoryID)
	}
	if req.Tag == "digest" {
		model = model.Where("digest > ?", 0)
	}
	if req.Tag == "hot" {
		model = model.Where("hot > ?", 0)
	}
	if req.Tag == "news" {
		order = "created_at desc"
	}
	if req.Username != "" {
		model = model.Where("username = ?", req.Username)
	}
	if req.Limit <= 0 {
		req.Limit = constutil.DefaultPageSize
	}

	if req.Limit > constutil.DefaultMaxPageSize {
		req.Limit = constutil.DefaultMaxPageSize
	}
	req.Page = req.Page - 1
	if req.Page <= 0 {
		req.Page = 0
	}
	model.Count(&count)
	err = model.Preload("Comment", func(db *gorm.DB) *gorm.DB {
		return db.Order("id DESC")
	}).Offset(req.Page * req.Limit).Order(order).Limit(req.Limit).Find(&threadList).Error
	return
}

// ThreadDetail  帖子详情
func (r *MysqlApiServeRepository) ThreadDetail(req *requests.ThreadDetail) (thread models.Thread, err error) {
	model := r.db.Model(&models.Thread{})
	order := "id desc"
	model = model.Where("id = ?", req.ThreadID)
	err = model.Order(order).First(&thread).Error
	return
}

// ThreadViewNum  查看数 +1
func (r *MysqlApiServeRepository) ThreadViewNum(threadID int, num int) (err error) {
	model := r.db.Model(models.Thread{})
	model = model.Where("id = ?", threadID)
	err = model.UpdateColumns(map[string]interface{}{
		"view_num": gorm.Expr("view_num + ?", num),
	}).Error
	return
}

// ThreadCommentNum  评论数 +1
func (r *MysqlApiServeRepository) ThreadCommentNum(threadID int, num int) (err error) {
	model := r.db.Model(models.Thread{})
	model = model.Where("id = ?", threadID)
	err = model.UpdateColumns(map[string]interface{}{
		"comment_num": gorm.Expr("comment_num + ?", num),
	}).Error
	return
}

// Thread  发表
func (r *MysqlApiServeRepository) Thread(req *requests.Thread) (err error) {
	model := r.db.Model(models.Thread{})
	value := models.Thread{
		Title:    req.Title,
		Content:  req.Content,
		Tag:      req.Tag,
		Username: req.Username,
		Avatar:   req.Avatar,
	}
	err = model.Create(&value).Error
	go func() {
		threadEs, err := elastic.NewThreadES(r.esClient)
		if err != nil {
			r.logger.Error("threadEs client", zap.Error(err))
			return
		}
		_, err = threadEs.Index(&elastic.ThreadEsRequest{
			Tag:      value.Tag,
			Username: value.Username,
			Avatar:   value.Avatar,
			ID:       strconv.Itoa(value.ID),
			Title:    value.Title,
		})
		r.logger.Error("threadEs Index Create", zap.Error(err))
	}()
	return
}

// ThreadList  帖子列表
func (r *MysqlApiServeRepository) UserThread(req *requests.UserThread) (count int64, threadList []*models.Thread, err error) {
	switch req.Tag {
	case "thread":
		count, threadList, err = r.ThreadList(&requests.ThreadList{
			Limit:    req.Limit,
			Page:     req.Page,
			Username: req.Username,
		})
		break
	case "comment":
		count, threadList, err = r.CommentThreadList(req)
		break
	case "collect":
		break
	}
	return
}
